﻿#include "NewTcpIOHandler.h"

#include "TcpListener.h"
#include "TcpCodec.h"
#include "TcpIOer.h"

NewTcpIOHandler::NewTcpIOHandler()
{

}

NewTcpIOHandler::~NewTcpIOHandler()
{

}

void NewTcpIOHandler::slot_newTcpConnection(TcpIOer* newTCPSocket)
{
  newTCPSocket->setCountDown(maxRegisterTime);//超时时间内没有注册则会断开
  connect(newTCPSocket,&TcpIOer::signal_dataReceived,tcpCodec,&TcpCodec::slot_decode,Qt::QueuedConnection);
}

TcpListener* NewTcpIOHandler::getTCPListener() const
{
  return tcpListener;
}

NewTcpIOHandler* NewTcpIOHandler::setTCPListener(TcpListener * const tcpListener)
{
  if(Q_NULLPTR!=this->tcpListener)
    {
      disconnect(this->tcpListener,Q_NULLPTR,this,Q_NULLPTR);
    }
  this->tcpListener=tcpListener;
  if(Q_NULLPTR!=this->tcpListener)
    {
      connect(this->tcpListener,&TcpListener::signal_newConnection,this,&NewTcpIOHandler::slot_newTcpConnection);
      connect(this->tcpListener,&TcpListener::destroyed,[this](){this->tcpListener=Q_NULLPTR;});
    }
  printInjection(this,tcpListener);
  return this;
}

TcpCodec* NewTcpIOHandler::getTcpCodec()const
{
  return tcpCodec;
}

NewTcpIOHandler* NewTcpIOHandler::setTcpCodec(TcpCodec * const tcpCodec)
{
  this->tcpCodec=tcpCodec;
  printInjection(this,tcpCodec);
  return this;
}

void NewTcpIOHandler::objectAutoInjection(QHash<QString,QObject*>* objMap)
{
  setTCPListener(getObjFromMap<TcpListener>(objMap));
  setTcpCodec(getObjFromMap<TcpCodec>(objMap));
}
